<!doctype html>
<html>
<head>
    <meta charset="UTF-8"/>
    <title>收件人信息</title>
    <link rel="stylesheet" type="text/css" href="/webapp/css/easyui.css">
    <link rel="stylesheet" type="text/css" href="/webapp/css/icon.css">
    <link rel="stylesheet" type="text/css" href="/webapp/css/color.css">
    <link rel="stylesheet" type="text/css" href="/webapp/css/public.css">

    <script src="http://i.gtimg.cn/qzone/biz/gdt/lib/jquery/jquery-2.1.4.js?max_age=31536000"></script>

    <script type="text/javascript" src="/webapp/js/lib/jquery.min.js"></script>
    <script src='https://cdn.bootcss.com/jquery.form/4.2.2/jquery.form.min.js'></script>
    <script type="text/javascript" src="/webapp/js/lib/jquery.easyui.min.js"></script>
    <script type="text/javascript" src="/webapp/js/lib/jquery.global.settings.js"></script>
    <script type="text/javascript" src="/webapp/js/lib/ajaxfileupload.js"></script>
    <script type="text/javascript" src="/webapp/js/lib/aliyun-oss-sdk.min.js"></script>
    <script type="text/javascript" src="/webapp/js/common/config.js"></script>
</head>
<body>
<div class="easyui-container-fluid">
    <div class="easyui-panel" style="border: none;">
        <div class="nav" id="nav"></div>
        <div class="easyui-panel" style="border:none;">
            <form id="ff">
                <div style="margin-bottom:20px;">
                    <label class="item-label" >企业名称：</label>
                    <select class="easyui-combobox" id="company"  style="width:100px;" name="company">
                    </select>
                    <label class="item-label" >负责人姓名：</label>
                    <select class="easyui-combobox" id="principal"  style="width:100px;" name="principalCode" data-options="multiple:true">
                    </select>
                    <label class="item-label" style="width: 50px;pxmargin-left: 20px;">年份：</label>
                    <input type="text" id="year" name="year" class="easyui-combobox" style="width: 100px;"/>
                    <label class="item-label">报告模式：</label>
                    <select  name="recordMode" class="easyui-combobox" id="recoedMode"data-options="panelHeight:'auto'" style="width:100px;">
                        <option value="OFFLINE">线下</option>
                        <option value="ONLINE">线上</option>
                    </select>
                    <label class="item-label">报告类型：</label>
                    <select  name="reportType" class="easyui-combobox"id="reportType" data-options="panelHeight:'auto'" style="width:100px;">
                        <option value="MONTH">月度报</option>
                        <option value="WEEK" selected>周度报</option>
                        <option value="QUARTER">季度报</option>
                        <option value="HYEAR">半年报</option>
                    </select>

                    <span id="monthLabel">
	                         <label class="item-label">月份：</label>
	                         <input type="text" class="easyui-combobox" name="month" id="month" style="width:100px;"/></span>
                    <span id="weekLabel">
	                         <label class="item-label">周数：</label>
	                         <input type="text" class="easyui-combobox" name="week" id="week"  style="width:100px;"/></span>
                    <span id="quarterLabel">
	                         <label class="item-label">季数：</label>
                            <select  name="q" class="easyui-combobox"  data-options="panelHeight:'auto'" style="width:100px;">
                                <option value="FIRSTQUARTER">第一季度</option>
                                <option value="SECONDQUARTER">第二季度</option>
                                <option value="YHIRDQUARTER">第三季度</option>
                                <option value="FOURTHQUARTER">第四季度</option>
                            </select>
                    </span>
                    <span id="yearLabel">
	                         <label class="item-label">半年数：</label>
	                          <select  name="y" class="easyui-combobox"  data-options="panelHeight:'auto'" style="width:100px;">
                                <option value="FIRSTHALF">上半年</option>
                                <option value="SEONDHALF">下半年</option>
                            </select>
                    </span>
                    <label class="item-label">状态：</label>
                    <input id="entityStatus" class="easyui-switchbutton" checked style="width:80px;height:30px;" onText="启用" offText="禁用"  >
                    <a href="javascript:void(0);" class="easyui-linkbutton c8" onclick="searchBy()" style="width:80px;margin-left: 40px;">查询</a>
                    <a href="javascript:void(0);" class="easyui-linkbutton c8" onclick="sendMail()" style="width:80px;margin-left: 40px;">发送邮件</a>
                </div>
            </form>
        </div>
        <div class="table-wrapper">
            <table id="datagrid" data-options="fitColumns:true,scrollBarSize:0">
                <thead>
                <tr>
                    <th data-options="field:'id',checkbox:true,align:'center'"></th>
                    <th data-options="field:'company',align:'center'" width="80">企业名</th>
                    <th data-options="field:'principalName',align:'center'" width="80">负责人姓名</th>
                    <th data-options="field:'title',align:'center'" width="80">邮件标题</th>
                    <th data-options="field:'proportion',align:'center'"  width="100">包含品类</th>
                    <th data-options="field:'operateUser',align:'center'" width="80">操作人</th>
                    <th data-options="field:'entityStatus',align:'center',formatter:statusFormatter" width="40">状态</th>
                    <th data-options="field:'action',align:'center',formatter:actions" width="100">操作</th>
                </tr>
                </thead>
                <tbody>
                </tbody>
            </table>
        </div>
    </div>
</div>
</body>
<script type="text/javascript">
    var id;
    function initPage(){
        searchBy(1,100);
    }
    $(function(){
        initPage();
        setNav();
        id=getQueryParameter("id");
        initYearSelect("year");
        initMonthSelect("month");
        initWeekSelect("week");
        initCompanyS("company", "principal");
        // $("#weekLabel").hide();
        $("#monthLabel").hide();
        $("#quarterLabel").hide();
        $("#yearLabel").hide();

        $("#reportType").combobox({
            onChange:function(n,o){
                //这里的参数n是select改变后的value,o是改变前的value
                if(o =="MONTH" ){
                    // recordMode="MONTH";
                    $("#monthLabel").hide();
                }else if (o == "WEEK"){

                    $("#weekLabel").hide();
                }else if(o == "QUARTER"){
                    $("#quarterLabel").hide();
                }else if(o == "HYEAR"){
                    $("#yearLabel").hide();
                }

                if(n =="MONTH" ){
                    $("#monthLabel").show();
                }else if (n == "WEEK"){
                    $("#weekLabel").show();
                }else if(n == "QUARTER"){
                    $("#quarterLabel").show();
                }else if(n == "HYEAR"){
                    $("#yearLabel").show();
                }
                // alert(n);
            }
        });
        var height = $(window).height();
        var nav_height = $("#nav")._outerHeight();
        var ff_height = $("#ff").height();
        var table_height = height-nav_height-ff_height-40;
        $("#datagrid").css("max-height",table_height);
        $("#datagrid").datagrid({
            pagination:true,
            rownumbers:true,
            idField:"id",
            checkOnSelect:true,
            toolbar:[{
                text:'添加数据',
                handler:function(){
                    window.parent.addTab("添加数据","/webapp/pages/emailR/add.html");
                }
            }],onLoadSuccess:function(data){
                $("#datagrid").datagrid("clearChecked");
            },
        }).datagrid("getPager").pagination({
            onSelectPage:searchBy
        });

    });

    function searchBy(page,size) {
        var dtd = $.Deferred();
        var data = getFormData(document.getElementById("ff"));
        var pager = {page:page,size:size};
        if(!size){
            pager['page']= 1;
            pager['size'] = $("#datagrid").datagrid("getPager").pagination("options").pageSize||100;
        }
        var jsonobj = {};
        var principal = $("#principal").combobox('getValues');

        jsonobj.entityStatus = $("#entityStatus").switchbutton("options").checked==true?"OK":"DISABLE";
        jsonobj = $.extend(true,jsonobj,transfer(data));
        jsonobj.principalCode = principal;
        jsonobj = JSON.stringify(jsonobj);
        // console.log(jsonobj);
        $.ajax({
            url:config.url + "/emailReceiving/page?"+transfer2url(pager),
            type:'post',
            data:jsonobj,
            contentType:"application/json;charset=UTF-8",
            dataType:'json',
            xhrFields:{withCredentials:true},
            beforeSend:function () {
                $.messager.progress({
                    title:"请稍等",
                    interval:50,
                    text:"数据加载中..."
                });
            },
            complete:function () {
                $.messager.progress("close");
            },
            success:function (data) {
                $.messager.progress("close");
                if(data){
                    globalData = data;
                    $("#datagrid").datagrid("loadData",data);

                    dtd.resolve();
                }

            },
            error:function (XMLHttpRequest,textStatus,errorThrown) {

            }
        });
        return dtd.promise();
    }
    function sendMail() {
        var selectedRow = $("#datagrid").datagrid("getChecked");
        if (selectedRow != null && selectedRow.length > 0) {
            var ids = selectedRow.map(function (item, index) {
                return item.id;
            }).join(",");
            var data = getFormData(document.getElementById("ff"));

            var params = { year: data.year, ids: ids};
            if (data.reportType == "MONTH") {
                params.mw = data.month;
            } else if(data.reportType == "WEEK"){
                params.mw = data.week;
            }else if(data.reportType == "QUARTER"){
                params.qy = data.q;
            }else if(data.reportType == "HYEAR"){
                params.qy = data.y;
            }else{
                console.log("报告类型为空");
            }

            params.recordMode = data.recordMode
            params.reportType =data.reportType
            console.log(params);
            $.messager.confirm("提示", "您确定发送邮件吗?", function (r) {
                if (r) {
                    $.ajax({
                    url: config.url + "/emailReceiving/sendEmail/",
                        type:"POST",
                        data:transfer(params),

                        dataType:'json',
                        xhrFields:{withCredentials:true},
                    beforeSend: function () {
                        $.messager.progress({
                            title: "请稍等",
                            interval: 50,
                            text: "邮件发送中，请稍等..."
                        });
                    },
                    complete: function () {
                        $.messager.progress("close");
                    },
                    success: function (data) {
                        if(data.code == 200){
                            $.messager.alert("提示",data.msg,"info",function(){
                                searchBy();
                            });
                        }else{
                            $.messager.alert("错误",data.msg,"error");
                        }
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        $.messager.progress("close");
                    }
                })
                }


            });
        } else {
            $.messager.alert("提示", "请选择相关记录", "error");
            return false;
        }
    }
</script>
<!--
<script type="text/javascript" src="/webapp/js/common/pagelist.js"></script>
-->
<script type="text/javascript">
    function actions(val, row){
        var disableBtnText = row.entityStatus=='OK'?'禁用':"启用";
        return "<a href='javascript:void(0);' class='easyui-linkbutton l-btn l-btn-small' "+
            "style='width:60px;' onclick='edit(\""+row.id+"\")'>修改</a>&nbsp;&nbsp;"+
            "<a href='javascript:void(0);' class='easyui-linkbutton l-btn l-btn-small' style='width:60px;' onclick='updateStatus(\""+row.id+"\", \"" + row.entityStatus + "\")'>"+disableBtnText+"</a>";
    }
    function edit(id){
        window.parent.addTab("修改", "/webapp/pages/emailR/add.html?id="+id);
    }
    function updateStatus(id){
        $.messager.confirm("提示","您确定禁用/启用吗？",function (f) {
            if(f){
                $.ajax({
                    url:config.url+"/emailReceiving/"+id,
                    type:"DELETE",
                    dataType:"json",
                    data:JSON.stringify(id),
                    contentType:"application/json;charset=UTF-8",
                    xhrFields:{withCredentials:true},
                    beforeSend:function () {
                        $.messager.progress({
                            title:"请稍等",
                            interval:50,
                            text:"操作进行中..."
                        });
                    },
                    complete:function () {
                        $.messager.progress("close");
                        $.messager.alert("提示","操作成功","info",function () {
                            searchBy(1,100);
                        });
                    },
                    success:function (data) {
                        $.messager.alert("提示","操作成功","info",function () {
                            searchBy(1,100);
                        });
                    },
                    error:function (data) {

                    }
                });
            }
        });
    }


//改变年月季度报告类型
    function initMonthWeek(monthId,weekId) {
        initYearSelect("year");
        if(isMonthType()){
            //隐藏周
            if (weekId){
                $("#"+weekId).remove();
                return initMonthSelect("month");
            }
        }else {
            //隐藏月
            if(monthId){
                $("#monthLabel").remove();
                return initWeekSelect("week");
            }
        }
        return $.Deferred().resolve();
    }

</script>
</html>
